package org.jeecg.modules.report.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.report.entity.dal.ParamEntity;
import org.jeecg.modules.report.entity.dto.ClassExamContrastDto;
import org.jeecg.modules.report.entity.dto.GrandFormDto;
import org.jeecg.modules.report.entity.vo.GrandFromVo;
import org.jeecg.modules.report.service.IGrandFormService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: jeecg-boot
 * @description: 成绩单
 * @author: vic
 * @create: 2021-05-09 16:14
 */
@RestController
@RequestMapping("/grandForm")
@Slf4j
public class GrandFormController {

    @Autowired
    private IGrandFormService grandFormService;
    /**
     * 成绩单
     *
     * @param paramEntity
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> getGrand(ParamEntity paramEntity, HttpServletRequest req) {
        GrandFromVo grandFromVo=grandFormService.grandVo(paramEntity);
        return Result.ok(grandFromVo);
    }

    /**
     * 获取各班级成绩分布
     *
     * @param paramEntity
     * @param req
     * @return
     */
    @GetMapping(value = "/getClassContrast")
    public Result<?> getClassContrast(ParamEntity paramEntity, HttpServletRequest req) {
        List<ClassExamContrastDto> result=grandFormService.getClassExamContrast(paramEntity);
        return Result.ok(result);
    }
    /**
     * 获取某班级或课程每次考试情况
     *
     * @param paramEntity
     * @param req
     * @return
     */
    @GetMapping(value = "/getClassCourse")
    public Result<?> getClassCourse(ParamEntity paramEntity, HttpServletRequest req) {
        List<ClassExamContrastDto> result=grandFormService.getClassExamCourseStu(paramEntity);
        return Result.ok(result);
    }

    /**
     * 获取个人成绩近几次考试请款
     *
     * @param paramEntity
     * @param req
     * @return
     */
    @GetMapping(value = "/getCourseScore")
    public Result<?> getCourseScore(ParamEntity paramEntity, HttpServletRequest req) {

        return Result.ok(grandFormService.getCourseScore(paramEntity));
    }

    /**
     * 导出成绩单
     * @param request
     * @param response
     * @param paramEntity
     * @return
     */
    @RequestMapping("/export")
    public ModelAndView export(HttpServletRequest request, HttpServletResponse response,ParamEntity paramEntity){

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //获取数据
        GrandFromVo grandFromVo=grandFormService.grandVo(paramEntity);
        if(grandFromVo==null){
            return mv;
        }

        log.info(grandFromVo.getGrandList().toString());
        mv.addObject(NormalExcelConstants.FILE_NAME,"score"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, GrandFormDto.class);
        ExportParams exportParams=new ExportParams(paramEntity.getCourseId()+"-"+paramEntity.getExamTime() + "成绩单", "导出人:" + sysUser.getRealname(), "score");
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, grandFromVo.getGrandList());
        return mv;
    }
}
